package fyf.com.carddesign.main.admin;

import android.content.Intent;
import android.graphics.Canvas;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback;
import com.chad.library.adapter.base.listener.OnItemSwipeListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import fyf.com.carddesign.BaseActivity;
import fyf.com.carddesign.Bll.CardBll;
import fyf.com.carddesign.Dao.Card;
import fyf.com.carddesign.MyUtl.MyUtl;
import fyf.com.carddesign.MyUtl.Tuple.Tuple;
import fyf.com.carddesign.R;
import fyf.com.carddesign.adapter.CardAdapter;

/**
 * 管理员添加配置卡片页面
 * Created by yxz on 2017/10/24.
 */

public class AdminMainActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    private AdminMainActivity _parent;
    @BindView(R.id.tbb_tv_name)
    TextView _tvTitleName;
    @BindView(R.id.tbb_ll_iv)
    ImageView _ivAddBtn;//添加卡片按钮
    @BindView(R.id.ai_swipe_refresh)
    SwipeRefreshLayout _swipeRefresh;//下拉刷新控件
    @BindView(R.id.ai_rv)
    RecyclerView _rv;//recycleview对象

    private CardAdapter _adapter;
    private ItemDragAndSwipeCallback _dragCallback;
    private ItemTouchHelper _touchHelper;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _parent = AdminMainActivity.this;
        ButterKnife.bind(this);
        _tvTitleName.setText("卡片管理页面");
        _ivAddBtn.setImageResource(R.mipmap.add);
        initSetContent();
    }

    //加载数据内容
    private void initSetContent() {
        _rv.setLayoutManager(new LinearLayoutManager(_parent));
        //设置颜色
        _swipeRefresh.setColorSchemeResources(android.R.color.holo_blue_light,
                android.R.color.holo_red_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_green_light);
        //获取到初始化数据列表信息
        Tuple<String, List<Card>> list = CardBll.getInstance(_parent).selectAllCard();
        if (!list.get_t1().equals("")) {
            return;
        }
        if (list.get_t2().size() <= 0) {//判断是否为空
            return;
        }
        _adapter = new CardAdapter(list.get_t2());
        _dragCallback = new ItemDragAndSwipeCallback(_adapter);
        _touchHelper = new ItemTouchHelper(_dragCallback);
        _touchHelper.attachToRecyclerView(_rv);
        _dragCallback.setSwipeMoveFlags(ItemTouchHelper.START | ItemTouchHelper.END);
        _adapter.enableSwipeItem();
//        _adapter.enableDragItem(_touchHelper);//开启滑动删除事件
        _adapter.setOnItemSwipeListener(onItemSwipeListener);
        _adapter.setOnItemClickListener(onItemClickListener);
        _swipeRefresh.setOnRefreshListener(this);//下拉刷新设置监听器
        _rv.setAdapter(_adapter);
    }

    /**
     * 滑动事件监听器
     */
    private OnItemSwipeListener onItemSwipeListener = new OnItemSwipeListener() {
        @Override
        public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {

        }

        @Override
        public void clearView(RecyclerView.ViewHolder viewHolder, int pos) {

        }

        @Override
        public void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos) {
            Card cc = _adapter.getItem(pos);//获取当前被清除卡片的数据模型
            Log.d("itemSwiped", "卡片:" + cc.getName() + "被清除");
            Tuple<Boolean, String> tmp = CardBll.getInstance(_parent).adminDeleteCard(cc.getId());
            if (!tmp.get_t1()) {
                MyUtl.Toast(_parent, tmp.get_t2());
                return;
            }
            MyUtl.Toast(_parent, "卡片：" + cc.getName() + tmp.get_t2());
        }

        @Override
        public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive) {

        }
    };

    //点击事件
    private BaseQuickAdapter.OnItemClickListener onItemClickListener = new BaseQuickAdapter.OnItemClickListener() {
        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            Intent intent = new Intent(_parent, AlertCardActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("Card", (Card) adapter.getItem(position));
            intent.putExtras(bundle);
            startActivity(intent);
        }
    };

    /**
     * 添加按钮
     *
     * @param view
     */
    @OnClick(R.id.tbb_rl_right)
    public void OnAddClick(View view) {
        Intent intent = new Intent(_parent, AddCardActivity.class);
        startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.admin_index;
    }

    @OnClick(R.id.tbb_ll_back)
    public void onBack(View v) {
        _parent.finish();
    }

    /**
     * 下拉刷新
     */
    @Override
    public void onRefresh() {
        _swipeRefresh.setRefreshing(true);
        //获取到初始化数据列表信息
        Tuple<String, List<Card>> list = CardBll.getInstance(_parent).selectAllCard();
        if (!list.get_t1().equals("")) {
            return;
        }
        if (list.get_t2().size() <= 0) {//判断是否为空
            return;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        _swipeRefresh.setRefreshing(false);
        //不需要通过重新创建adapter对象进行数据添加！
        _adapter.setNewData(list.get_t2());

        //添加更多的信息
       // _adapter.addData();
    }
}
